<?php

    /**
     * This is the model class for table "{{customers}}".
     *
     * The followings are the available columns in table '{{customers}}':
     * @property integer $id
     * @property integer $user_id
     * @property string $customer_code
     * @property string $firstname
     * @property string $lastname
     * @property string $birthday
     * @property string $race
     * @property string $religion
     * @property string $newic
     * @property string $address1
     * @property string $address2
     * @property string $postcode
     * @property string $city
     * @property string $state
     * @property string $country
     * @property string $sec_address1
     * @property string $sec_address2
     * @property string $sec_postcode
     * @property string $sec_city
     * @property string $sec_state
     * @property string $sec_country
     * @property string $guarantee_firstname
     * @property string $guarantee_lastname
     * @property string $guarantee_birthday
     * @property string $guarantee_newic
     * @property string $guarantee_address1
     * @property string $guarantee_address2
     * @property string $guarantee_postcode
     * @property string $guarantee_city
     * @property string $guarantee_state
     * @property string $guarantee_country     
     * @property string $createdate
     * @property string $modifydate
     * @property integer $updateby_user_id
     * @property integer $last_loan_num
     * @property integer $gallery_id
     */
    class Customers extends CActiveRecord {

        /**
         * @return string the associated database table name
         */
        public function tableName() {
            return '{{customers}}';
        }

        /**
         * @return array validation rules for model attributes.
         */
        public function rules() {
            // NOTE: you should only define rules for those attributes that
            // will receive user inputs.
            return array(
                array('user_id, customer_code, firstname, lastname, birthday, race, religion, newic, address1, postcode, city, state, country, sec_address1, sec_postcode, sec_city, sec_state, sec_country, guarantee_firstname, guarantee_lastname, guarantee_birthday, guarantee_newic, guarantee_address1, guarantee_postcode, guarantee_city, guarantee_state, guarantee_country, updateby_user_id', 'required'),
                array('customer_code', 'unique', 'message' => t('This Customer Code already exists. Please contact your software vendor.')),
                array('birthday', 'match', 'pattern' => '/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/'),
                array('firstname, lastname, race, religion, city, state, country, sec_city, sec_state, sec_country, guarantee_firstname, guarantee_lastname, guarantee_city, guarantee_state, guarantee_country', 'length', 'max' => 50),
                array('newic, guarantee_newic', 'length', 'max' => 16),
                array('newic', 'unique', 'message' => t('This IC already exists.')),
                array('newic, guarantee_newic', 'match', 'pattern' => '/^[0-9]{8}-[0-9]{2}-[0-9]{4}$/', 'message' => 'Format: 00000000-00-0000'),
                //array('guarantee_newic', 'pattern' => '/^[0-9]{8}-[0-9]{2}-[0-9]{4}$/', 'message' => '00000000-00-0000'),
                array('address1, address2, sec_address1, sec_address2, guarantee_address1, guarantee_address2', 'length', 'max' => 100),
                array('postcode, sec_postcode, guarantee_postcode', 'length', 'max' => 5),
                array('postcode, sec_postcode, guarantee_postcode', 'match', 'pattern' => '/^[0-9]{5}$/', 'message' => 'Format: 12345'),
                array('createdate', 'safe'),
                // The following rule is used by search().
                // @todo Please remove those attributes that should not be searched.
                array('id, user_id, customer_code, firstname, lastname, birthday, race, religion, newic, address1, address2, postcode, city, state, country, sec_address1, sec_address2, sec_postcode, sec_city, sec_state, sec_country, guarantee_firstname, guarantee_lastname, guarantee_birthday, guarantee_newic, guarantee_address1, guarantee_address2, guarantee_postcode, guarantee_city, guarantee_state, guarantee_country, createdate, modifydate, updateby_user_id, last_loan_num, gallery_id', 'safe', 'on' => 'search'),
            );
        }

        /**
         * @return array relational rules.
         */
        public function relations() {
            // NOTE: you may need to adjust the relation name and the related
            // class name for the relations automatically generated below.
            return array(
                'customerLoan' => array(self::HAS_MANY, 'CustomerLoanSetting', 'customer_id'),
                'user' => array(self::HAS_ONE, 'application.modules.user.models.User', 'user_id'),
                'gallery' => array(self::HAS_ONE, 'ext.galleryManager.Gallery', 'gallery_id'),
            );
        }

        /**
         * @return array customized attribute labels (name=>label)
         */
        public function attributeLabels() {
            return array(
                'id' => 'ID',
                'user_id' => t('User ID'),
                'customer_code' => t('Customer Code'),
                'firstname' => t('First Name'),
                'lastname' => t('Last Name'),
                'birthday' => t('Birthday'),
                'race' => t('Race'),
                'religion' => t('Religion'),
                'newic' => t('New IC'),
                'address1' => t('Address') . '1',
                'address2' => t('Address') . '2',
                'postcode' => t('Post Code'),
                'city' => t('City'),
                'state' => t('State'),
                'country' => t('Country'),
                'sec_address1' => t('Address') . '1',
                'sec_address2' => t('Address') . '2',
                'sec_postcode' => t('Post Code'),
                'sec_city' => t('City'),
                'sec_state' => t('State'),
                'sec_country' => t('Country'),
                'guarantee_firstname' => t('First Name'),
                'guarantee_lastname' => t('Last Name'),
                'guarantee_birthday' => t('Birthday'),
                'guarantee_newic' => t('New IC'),
                'guarantee_address1' => t('Address') . '1',
                'guarantee_address2' => t('Address') . '2',
                'guarantee_postcode' => t('Post Code'),
                'guarantee_city' => t('City'),
                'guarantee_state' => t('State'),
                'guarantee_country' => t('Country'),
                'createdate' => t('Create Date'),
                'modifydate' => t('Modify Date'),
                'updateby_user_id' => t('Updated by User ID'),
            );
        }

        /**
         * Retrieves a list of models based on the current search/filter conditions.
         *
         * Typical usecase:
         * - Initialize the model fields with values from filter form.
         * - Execute this method to get CActiveDataProvider instance which will filter
         * models according to data in model fields.
         * - Pass data provider to CGridView, CListView or any similar widget.
         *
         * @return CActiveDataProvider the data provider that can return the models
         * based on the search/filter conditions.
         */
        public function search() {
            // @todo Please modify the following code to remove attributes that should not be searched.

            $criteria = new CDbCriteria;

            $criteria->compare('id', $this->id);
            $criteria->compare('user_id', $this->user_id);
            $criteria->compare('customer_code', $this->customer_code, true);
            $criteria->compare('firstname', $this->firstname, true);
            $criteria->compare('lastname', $this->lastname, true);
            $criteria->compare('birthday', $this->birthday, true);
            $criteria->compare('race', $this->race, true);
            $criteria->compare('religion', $this->religion, true);
            $criteria->compare('newic', $this->newic, true);
            $criteria->compare('address1', $this->address1, true);
            $criteria->compare('address2', $this->address2, true);
            $criteria->compare('postcode', $this->postcode, true);
            $criteria->compare('city', $this->city, true);
            $criteria->compare('state', $this->state, true);
            $criteria->compare('country', $this->country, true);
            $criteria->compare('sec_address1', $this->sec_address1, true);
            $criteria->compare('sec_address2', $this->sec_address2, true);
            $criteria->compare('sec_postcode', $this->sec_postcode, true);
            $criteria->compare('sec_city', $this->sec_city, true);
            $criteria->compare('sec_state', $this->sec_state, true);
            $criteria->compare('sec_country', $this->sec_country, true);
            $criteria->compare('guarantee_firstname', $this->guarantee_firstname, true);
            $criteria->compare('guarantee_lastname', $this->guarantee_lastname, true);
            $criteria->compare('guarantee_birthday', $this->guarantee_birthday, true);
            $criteria->compare('guarantee_newic', $this->guarantee_newic, true);
            $criteria->compare('guarantee_address1', $this->guarantee_address1, true);
            $criteria->compare('guarantee_address2', $this->guarantee_address2, true);
            $criteria->compare('guarantee_postcode', $this->guarantee_postcode, true);
            $criteria->compare('guarantee_city', $this->guarantee_city, true);
            $criteria->compare('guarantee_state', $this->guarantee_state, true);
            $criteria->compare('guarantee_country', $this->guarantee_country, true);
            $criteria->compare('createdate', $this->createdate, true);
            $criteria->compare('modifydate', $this->modifydate, true);
            $criteria->compare('updateby_user_id', $this->updateby_user_id, true);
            $criteria->compare('last_loan_num', $this->last_loan_num, true);

            return new CActiveDataProvider($this, array(
                'criteria' => $criteria,
            ));
        }

        /**
         * Returns the static model of the specified AR class.
         * Please note that you should have this exact method in all your CActiveRecord descendants!
         * @param string $className active record class name.
         * @return Customers the static model class
         */
        public static function model($className = __CLASS__) {
            return parent::model($className);
        }

        public function behaviors() {
            return array(
                'galleryBehavior' => array(
                    'class' => 'ext.galleryManager.GalleryBehavior',
                    'idAttribute' => 'gallery_id',
                    'versions' => array(
                        'small' => array(
                            'centeredpreview' => array(98, 98),
                        ),
                        'medium' => array(
                            'resize' => array(800, null),
                        )
                    ),
                    'name' => true,
                    'description' => true,
                ),
            );
        }

    }

